'''
Created on 21-Apr-2013

@author: cdac
'''


from SMS.PreprocessSMS import PreprocessSMS
from Plotting.StatsPlot import StatsPlot
from Experiments.Exp import Exp

class MaxSizeExp(Exp):
    '''
    classdocs
    '''
    def __init__(self, ham_source, spam_source):
        '''
        Constructor
        '''
        self.set_file_sources(ham_source, spam_source)
        self.init_data_sources()
        
    def experiment(self):
        pass
        
if __name__ == '__main__':
 
    max_size_exp = MaxSizeExp('../Data/Ham.csv', '../Data/Spam.csv')
#     max_size_exp = MaxSizeExp('../Data/HamSmall.csv', '../Data/SpamSmall.csv')
 
    # Initialize the list to hold maximum msg size stats     
    # Raw msgs   
    ham_max_stat_raw = list()
    spam_max_stat_raw = list()
    
    # Preprocessed msgs (removed punctuations and special characters
    # and also lower cased
    ham_max_stat_prep = list()
    spam_max_stat_prep = list()

    # no stop words
    ham_max_stat_no_sw = list()
    spam_max_stat_no_sw = list()
  
    exp_range = range(0, 500, 20)
        
    for sample in exp_range:
        # Get first 'sample size' msgs from ham and spam
        hams    = max_size_exp.get_first_msgs_from_ham(sample)
        spams   = max_size_exp.get_first_msgs_from_spam(sample)
        
        # Extract Stats
        max_size_exp.extract_ham_stats(hams, "HamSample" + str(sample))
        max_size_exp.extract_spam_stats(spams, "SpamSample" + str(sample))
    
        ham_max_stat_raw.append(max_size_exp.get_stats_for_hams().max_msg_size())
        spam_max_stat_raw.append(max_size_exp.get_stats_for_spam().max_msg_size())
    
        # Preprocess ham and spam by removing punctuations and
        # special characters
        processor = PreprocessSMS()
          
        hams = processor.remove_puncts_and_special_chars(hams)
        hams = processor.convert_to_lowercase(hams)
    
        spams = processor.remove_puncts_and_special_chars(spams)
        spams = processor.convert_to_lowercase(spams)
        
        # Recalculate Stats of pre-processed samples
        max_size_exp.extract_ham_stats(hams, "HamSamplePrep" + str(sample))
        max_size_exp.extract_spam_stats(spams, "SpamSamplePrep" + str(sample))
    
        ham_max_stat_prep.append(max_size_exp.get_stats_for_hams().max_msg_size())
        spam_max_stat_prep.append(max_size_exp.get_stats_for_spam().max_msg_size())
      
        # Remove all stopwords from samples
        hams = processor.remove_stopwords(hams)
        spams = processor.remove_stopwords(spams)
    
        # Recalculate Stats of samples without stopwords        
        max_size_exp.extract_ham_stats(hams, "HamSampleSW" + str(sample))
        max_size_exp.extract_spam_stats(spams, "SpamSampleSw" + str(sample))
    
        ham_max_stat_no_sw.append(max_size_exp.get_stats_for_hams().max_msg_size())
        spam_max_stat_no_sw.append(max_size_exp.get_stats_for_spam().max_msg_size())

    print "\n" + 30*'*' + "Ham Stats" + 30*'*'    
    print "Maximum size of Ham Message:\t\t\t\t", max(ham_max_stat_raw)
    print "Maximum size of Preprocessed Ham Message:\t\t", max(ham_max_stat_prep)
    print "Maximum size of Ham Message without Stopwords:\t\t", max(ham_max_stat_no_sw)
    
    print "\n" + 30*'*' + "Spam Stats" + 30*'*'
    print "Maximum size of Spam Message:\t\t\t\t", max(spam_max_stat_raw)
    print "Maximum size of Preprocessed Spam Message:\t\t", max(spam_max_stat_prep)
    print "Maximum size of Spam Message without Stopwords:\t\t", max(spam_max_stat_no_sw)
        
    statsPlot = StatsPlot()
    statsPlot.max_plot(exp_range, ham_max_stat_raw, ham_max_stat_prep, ham_max_stat_no_sw, spam_max_stat_raw, spam_max_stat_prep, spam_max_stat_no_sw)